package com.alex.util.security.sha1;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * @author alex
 */
public class SHA1 {
    public SHA1() {
    }

    public static String getSHA1(TreeMap paramsMap) {
        StringBuffer sb = new StringBuffer();
        if (paramsMap != null && paramsMap.size() != 0) {
            Iterator it = paramsMap.entrySet().iterator();

            while (it.hasNext()) {
                Entry entry = (Entry) it.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                sb.append(key).append(value);
            }

            String str = sb.toString();
            MessageDigest md = null;

            try {
                md = MessageDigest.getInstance("SHA-1");
            } catch (NoSuchAlgorithmException var9) {
                var9.printStackTrace();
            }

            md.update(str.getBytes());
            byte[] digest = md.digest();
            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";

            for (int i = 0; i < digest.length; ++i) {
                shaHex = Integer.toHexString(digest[i] & 255);
                if (shaHex.length() < 2) {
                    hexstr.append(0);
                }

                hexstr.append(shaHex);
            }

            return hexstr.toString();
        } else {
            return "";
        }
    }

}

